package com.itheima.day15多线程;
/*4、【编号2004】有一个抽奖池，里面存放了一些固定金额的奖金
int[] prizePool = {100, 200, 300, 400, 500, 600, 700, 800,
 900, 1000};此时，有两个人A和B轮流在奖箱中随机抽奖
 （即A抽一次-->B抽一次-->A抽一次-->B抽一次...），
 直到所有的奖金都被抽出，每次抽奖耗时1秒。请编写程序，
 使用两个线程模拟两人的抽奖过程，输出结果如下（金额的顺序随机）：
A抽出奖金600
B抽出奖金1000
A抽出奖金900
B抽出奖金300
A抽出奖金500
B抽出奖金200
A抽出奖金700
B抽出奖金100
A抽出奖金800
B抽出奖金400*/

import java.util.ArrayList;
import java.util.Random;

public class Demo4x {
    public static boolean flag = false;

    public static ArrayList<Integer> getList() {
        ArrayList<Integer> list = new ArrayList<>();
        int[] prizePool = {100, 200, 300, 400,
                500, 600, 700, 800, 900, 1000};
        for (int i = 0; i < prizePool.length; i++) {
            list.add(prizePool[i]);
        }
        return list;
    }
}

class Testx {
    public static void main(String[] args) {
        ArrayList<Integer> list = Demo4x.getList();
        Random ran = new Random();
        new Thread(() ->
        {
            synchronized (Testx.class) {
                while (list.size() > 0) {
                    synchronized (Testx.class) {
                        if (!Demo4x.flag) {
                            int number = ran.nextInt(list.size());
                            System.out.println(Thread.currentThread().getName() + "抽出奖金" + list.get(number));
                            Demo4x.flag = true;
                            Testx.class.notifyAll();
                            list.remove(number);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                //e.printStackTrace();
                            }
                        } else {
                            try {
                                Testx.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }, "A").start();


        new Thread(() ->
        {
            synchronized (Testx.class) {
                while (list.size() != 0) {
                    synchronized (Testx.class) {
                        if (Demo4x.flag) {
                            int number = ran.nextInt(list.size());
                            System.out.println(Thread.currentThread().getName() + "抽出奖金" + list.get(number));
                            Demo4x.flag = false;
                            Testx.class.notifyAll();
                            list.remove(number);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                //e.printStackTrace();
                            }
                        } else {
                            try {
                                Testx.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }, "B").start();


    }
}